<?php

/**
 * @author chenshubo
 * 查询渠道商订单结算金额详情接口
 */
namespace App\Service\OrderService\OrderInterfaceService;

use App\Model\PssModel\OrderSettlement;
use App\Service\BaseService;
use App\Service\Code\Normal;

class QueryChannelOrderSettlement extends BaseService
{
    /**
     * 查询渠道商订单结算金额详情接口
     * @param array $param
     * @return mixed
     */
    public function index( array $param = [] )
    {
        $requestData = $param['requestData'];
        $bid = $param['binfo']['bid'];
        if (empty($requestData['orderSn'])){
            return Normal::returnCode(Normal::FAIL, "except param requestData.orderSn");
        }

        if (empty($requestData['orderNo'])){
            return Normal::returnCode(Normal::FAIL, "except param requestData.orderNo");
        }
        
        $cacheKey = md5(http_build_query($requestData));
        $cacheKey = "WEB_QUERYCHANNELORDERSETTLEMENT_{$bid}_".$cacheKey;
        $redis    = $this->getRedisObj();
        $redis->select(self::rdb_num);
        $info = $redis->get($cacheKey);
        if( !empty($info) ) {
            return Normal::returnCode(Normal::SUC,"获取成功",[
                'info' => json_decode($info,true)
            ]);
        }
        
        $orderSn = $requestData['orderSn'];
        $orderNo = $requestData['orderNo'];
        $where = [
            'bid'               => $bid,
            'order_sn'          => $orderSn,
            'supplier_csn'      => $orderNo,
            'settlement_type'   => 1,
            'settlement_mode'   => 1,
        ];
        $row = OrderSettlement::instance()->select(['goods_money','tax_money','delivery_money','settlement_money'])->where($where)->first();
        if (empty($row)){
            return Normal::returnCode(Normal::FAIL,"结算数据还未生成，请稍后重试！");
        }
        
        $info = [
            'orderSn'           => (string)$orderSn,
            'orderNo'           => (string)$orderNo,
            'goodsMoney'        => (string)$row->goods_money,
            'taxMoney'          => (string)$row->tax_money,
            'deliveryMoney'     => (string)$row->delivery_money,
            'settlementMoney'   => (string)$row->settlement_money
        ];
        
        //更新查询结果到缓存
        $redis->set($cacheKey,json_encode($info),self::CACHE_TIME);
        return Normal::returnCode(Normal::SUC,"获取成功",[
            'info' => $info
        ]);
    }
}